package com.example.server.impl;

import com.example.dao.dictMapper.DictMapper;
import com.example.dto.dict.Dict;
import com.example.dto.dict.DictCriteria;
import com.example.server.DictServer;
import com.example.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "dict")
public class DictServerImpl implements DictServer {
    @Autowired
    private DictMapper dictMapper;

    /**
     * 新增字典
     *
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Dict resources) {
        dictMapper.creat(resources);
    }
    /**
     * 删除字典
     *
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Set<Long> ids) {
        dictMapper.deleteByIdIn(ids);
    }

    /**
     * 修改菜单
     *
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Dict resources) {
        Dict dict = dictMapper.findById(resources.getId());
        ValidationUtil.isNull( dict.getId(),"Dict","id",resources.getId());
        dict.setName(resources.getName());
        dict.setDescription(resources.getDescription());
        dictMapper.save(dict);
    }
    /**
     * 查找字典
     *
     * @param dictCriteria
     * @return
     */
    @Override
    public Map<String, Object> list(DictCriteria dictCriteria) {
        Map<String, Object> result = new HashMap<>(4);

        Map<String, Object> css = new HashMap<>();
        css.put("createTime", null);
        css.put("updateTime", null);
        css.put("blurry", dictCriteria.getBlurry());
        css.put("page", dictCriteria.getPage());
        css.put("size", dictCriteria.getSize());

        List<Dict> dicts = dictMapper.selectList(css);

        int count = dictMapper.findCount(css);
        result.put("content", dicts);
        result.put("totalElements", count);
        return result;
    }

    /**
     * 查找全部字典
     *
     * @param dict
     * @return
     */
    @Override
    public Map<String, Object> findAll(Dict dict) {
        Map<String, Object> result = new HashMap<>(4);
        Map<String, Object> css = new HashMap<>();
        List<Dict> dicts = dictMapper.findAll(dict);
        result.put("content", dicts);
        return result;
    }
}
